package com.zui.product.controller;

import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.zui.common.domain.PageParam;
import com.zui.common.domain.Result;
import com.zui.product.domain.vo.brand.*;
import com.zui.product.service.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 品牌 Controller
 * @version 2021-05-05
 */
@Slf4j
@Api(tags="品牌接口")
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @PostMapping(value = "/add")
    @ApiOperation(value = "品牌-添加", notes = "品牌-添加")
    public Result<BrandVO> add(@RequestBody AddBrandVO addBrandVO) {
        log.info("品牌-添加入参:", JSONUtil.toJsonStr(addBrandVO));
        try {
            brandService.add(addBrandVO);
            return Result.success();
        } catch (Exception e) {
            log.info("品牌-添加异常: {}", e.getMessage());
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    @GetMapping(value = "/get")
    @ApiOperation(value = "品牌-详情查询", notes = "品牌-详情查询")
    public Result<BrandVO> get(String id) {
        log.info("品牌-详情查询入参: id = ", id);
        try {
            BrandVO result = brandService.get(id);
            return Result.success(result);
        } catch (Exception e) {
            log.info("品牌-详情查询异常: {}", e.getMessage());
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    @GetMapping(value = "/page")
    @ApiOperation(value = "品牌-分页查询", notes = "品牌-分页查询")
    public Result<PageInfo<BrandVO>> list(PageParam<ListBrandVO> param) {
        log.info("品牌-分页查询入参: ", JSONUtil.toJsonStr(param));

		try {
			PageInfo<BrandVO> result = brandService.page(param);
			return Result.success(result);
        } catch (Exception e) {
            log.info("品牌-分页查询异常: {}", e.getMessage());
            e.printStackTrace();
			return Result.fail(e.getMessage());
        }
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "品牌-更新", notes = "品牌-更新")
    public Result<BrandVO> update(@RequestBody UpdateBrandVO updateBrandVO) {
        log.info("品牌-更新入参:", JSONUtil.toJsonStr(updateBrandVO));
        try {
            brandService.update(updateBrandVO);
            return Result.success();
        } catch (Exception e) {
            log.info("品牌-更新异常: {}", e.getMessage());
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping(value = "/delete")
    @ApiOperation(value = "品牌-删除", notes = "品牌-删除")
    public Result<BrandVO> delete(@RequestBody DeleteBrandVO deleteBrandVO) {
        log.info("品牌-删除入参:", JSONUtil.toJsonStr(deleteBrandVO));
        try {
            brandService.delete(deleteBrandVO);
            return Result.success();
        } catch (Exception e) {
            log.info("品牌-删除异常: {}", e.getMessage());
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

}